package com.heima.article.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.mapper.ArticleConfigMapper;
import com.heima.article.mapper.ArticleContentMapper;
import com.heima.article.mapper.ArticleMapper;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.article.pojos.ArticleDoc;
import com.heima.model.common.constants.ArticleConstants;
import com.heima.model.common.constants.WmNewsMessageConstants;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.Wmaparticle;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DownOrUpListener {

    @Autowired
    private ArticleConfigMapper articleConfigMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleContentMapper articleContentMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /*
    监听文章上下架功能
     */

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_QUEUE,durable = "true"),
                    exchange = @Exchange(name = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC,type = ExchangeTypes.TOPIC),
                    key = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_ROUTINGKEY
            )
    )
    public void insertMessage(Wmaparticle apArticle){
        //TODO
        ApArticle bean = BeanUtil.toBean(apArticle, ApArticle.class);
        bean.setAuthorId(Long.valueOf(apArticle.getAuthorId()));
        bean.setAuthorName(apArticle.getAuthorName());
        bean.setLayout(apArticle.getType());
        articleMapper.insert(bean);
        ArticleDoc articleDoc = new ArticleDoc(bean);
        articleDoc.setContent(apArticle.getContent());
        rabbitTemplate.convertAndSend(ArticleConstants.ARTICLE_ES_SYNC_TOPIC
                , ArticleConstants.ARTICLE_ES_SYNC_ROUTINGKEY
                , articleDoc);

        if (!ObjectUtil.isEmpty(bean.getId())){
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setIsDown(true);
            apArticleConfig.setArticleId(bean.getId());
            apArticleConfig.setIsDelete(false);
            apArticleConfig.setIsComment(true);
            apArticleConfig.setIsForward(true);
            articleConfigMapper.insert(apArticleConfig);
            ApArticleContent articleContent = new ApArticleContent();
            articleContent.setArticleId(bean.getId());
            articleContent.setContent(apArticle.getContent());
            articleContentMapper.insert(articleContent);

        }
    }
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_QUEUE,durable = "true"),
                    exchange = @Exchange(name = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC,type = ExchangeTypes.TOPIC),
                    key = "keyiu"
            )
    )
    public void getMessage(WmNews wmNews){
        LambdaUpdateWrapper<ApArticle> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ApArticle::getTitle,wmNews.getTitle());
        ApArticle apArticle = articleMapper.selectOne(lambdaUpdateWrapper);
        Long id = apArticle.getId();
        LambdaUpdateWrapper<ApArticleConfig> lambdaUpdateWrapper1=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper1.eq(ApArticleConfig::getArticleId,id);
        if (wmNews.getEnable()==1){
            lambdaUpdateWrapper1.set(ApArticleConfig::getIsDown,0);
        }else {
            lambdaUpdateWrapper1.set(ApArticleConfig::getIsDown,1);
        }

        articleConfigMapper.update(new ApArticleConfig(),lambdaUpdateWrapper1);
    }



}
